#include "autostatus.h"

GtkWidget *time_select_menu_new()
{
    GtkWidget *input  = gtk_entry_new();
    char ts[10];
    GDateTime *now = g_date_time_new_now_local(); 
    sprintf(ts, "%02d:%02d", g_date_time_get_hour(now), g_date_time_get_minute(now));
    gtk_entry_set_text(input, ts);
    g_date_time_unref(now);
    return input;
}


GtkWidget *time_interval_select_menu_new()
{
    GtkWidget *combobox;
	GtkListStore *model;
	GtkTreeIter iter;
	GtkCellRenderer *text_rend;

	combobox = gtk_combo_box_new();

	model = gtk_list_store_new(TIME_INTERVAL_COLUMNS, G_TYPE_STRING, G_TYPE_INT);

    gtk_combo_box_set_model(GTK_COMBO_BOX(combobox), GTK_TREE_MODEL(model));

    gtk_list_store_append (model, &iter);
    gtk_list_store_set (model, &iter,
                    TIME_INTERVAL_NAME, "day",
                    TIME_INTERVAL_VALUE, TIME_INTERVAL_EVERYDAY,
                    -1);

    gtk_list_store_append (model, &iter);
    gtk_list_store_set (model, &iter,
                    TIME_INTERVAL_NAME, "work day",
                    TIME_INTERVAL_VALUE, TIME_INTERVAL_WORKDAY,
                    -1);

    gtk_list_store_append (model, &iter);
    gtk_list_store_set (model, &iter,
                    TIME_INTERVAL_NAME, "weekend",
                    TIME_INTERVAL_VALUE, TIME_INTERVAL_WEEKEND,
                    -1);

    text_rend = gtk_cell_renderer_text_new();
	gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combobox), text_rend, TRUE);
	gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combobox), text_rend, "markup", TIME_INTERVAL_NAME, NULL);
	g_object_set(text_rend, "ellipsize", PANGO_ELLIPSIZE_END, NULL);

    gtk_combo_box_set_active(GTK_COMBO_BOX(combobox), 0);

    return combobox;
}

void active_status_by_time(AutoStatus *plg)
{
    DEBUG("active status by time");

    GDateTime *now = g_date_time_new_now_local();    
    GDateTime *start =  g_date_time_new_local(
            g_date_time_get_year(now),
            g_date_time_get_month(now),
            g_date_time_get_day_of_month(now),
            0,
            0,
            0);

    int now_seconds = g_date_time_to_unix(now) - g_date_time_to_unix(start); 
    DEBUG("now seconds:%d", now_seconds);

    g_date_time_unref(start);

    int week_day = g_date_time_get_day_of_week(now);

    ConfigRule *_tmp_rule = plg->rules;
    while(_tmp_rule != NULL)
    {
        int offset = now_seconds - _tmp_rule->time_seconds;
        if (_tmp_rule->rule_type == RULE_TYPE_TIME && offset >= 0 
                && offset <= TIMEOUT_CHECK_INTERVAL)
        {
            DEBUG("it is the time");
            if (_tmp_rule->time_interval == TIME_INTERVAL_WORKDAY && (week_day == 6 || week_day == 7)) 
            {
                _tmp_rule = _tmp_rule->next; 
                continue;
            }

            if (_tmp_rule->time_interval == TIME_INTERVAL_WEEKEND && week_day != 6 && week_day != 7) 
            {
                _tmp_rule = _tmp_rule->next; 
                continue;
            }

            PurpleSavedStatus *status = get_saved_status(_tmp_rule->status_type, _tmp_rule->status_key);
            if (status != NULL)
            {
                purple_savedstatus_activate(status); 
                break;
            }
            else
            {
                DEBUG("saved status not found"); 
            }
        }
        _tmp_rule = _tmp_rule->next; 
    }

    g_date_time_unref(now);
}
